from mcp import ClientSession, StdioServerParameters, types
import asyncio
import os

# AnyUrl用于资源URL的类型校验
from pydantic import AnyUrl

from mcp.client.stdio import stdio_client


async def main() -> None:
    base_dir = os.path.dirname(os.path.abspath(__file__))
    server_path = os.path.join(base_dir, "server.py")
    server_params = StdioServerParameters(command="python", args=[server_path], env={})
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()
            resources = await session.list_resources()
            print(f"[resources]", [r.uri for r in resources.resources])

            # templates = await session.list_resource_templates()
            # print(
            #    f"[resource_template]",
            #    [r.uriTemplate for r in templates.resourceTemplates],
            # )
            result = await session.read_resource(AnyUrl("config://settings"))
            texts = []
            for block in result.contents:
                if isinstance(block, types.TextResourceContents):
                    texts.append(block.text)
            print(f"[Read config://settings]", "|".join(texts))

            # result = await session.read_resource(AnyUrl("greeting://Alice"))
            # texts = []
            # for block in result.contents:
            #    if isinstance(block, types.TextResourceContents):
            #        texts.append(block.text)
            # print(f"[Read greeting://Alice]", "|".join(texts))


#
# result = await session.read_resource(AnyUrl("user://002"))
# texts = []
# for block in result.contents:
#    if isinstance(block, types.TextResourceContents):
#        texts.append(block.text)
# print(f"[user://002]", "|".join(texts))


if __name__ == "__main__":
    asyncio.run(main())
